package com.hundsun.documentripper.mapper;

import com.baomidou.mybatisplus.annotation.SqlParser;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hundsun.documentripper.domain.dto.UserLogView;
import com.hundsun.documentripper.domain.pojo.UserLog;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * Note：
 *
 * @author ：mengjw
 * @description：用户操作日志Mapper
 * @date ：Created in 2021/1/26
 */
@SqlParser(filter  =  true)
public interface UserLogMapper extends BaseMapper<UserLog> {

    @Select("select l.*, \n" +
            "u.name as user_name, \n" +
            "p.name as product_name, \n" +
            "pv.code as product_version_code, \n" +
            "IFNULL(f.name, h.name) as file_name \n" +
            "from user_log l \n" +
            "left join user u on l.user_uuid = u.uuid \n" +
            "left join product p on l.product_uuid = p.uuid \n" +
            "left join product_version pv on l.product_version_uuid = pv.uuid \n" +
            "left join file f on l.file_uuid = f.uuid \n" +
            "left join file_history h on l.file_uuid = h.uuid \n" +
            "${ew.customSqlSegment}")
    IPage<UserLogView> selectLogView(Page<UserLogView> page, @Param(Constants.WRAPPER) Wrapper<UserLogView> wrapper);

    @Select("select l.*, \n" +
            "u.name as user_name,\n" +
            "p.name as product_name,\n" +
            "pv.code as product_version_code,\n" +
            "IFNULL(f.name, h.name) as file_name \n" +
            "from user_log l\n" +
            "right join (select a.document_uuid, max(a.create_time) as create_time \n" +
            "from user_log a\n" +
            "group by a.document_uuid ) b on l.document_uuid = b.document_uuid and  l.create_time  =  b.create_time\n" +
            "left join user u on l.user_uuid = u.uuid \n" +
            "left join product p on l.product_uuid = p.uuid \n" +
            "left join product_version pv on l.product_version_uuid = pv.uuid \n" +
            "left join file f on l.file_uuid = f.uuid \n" +
            "left join file_history h on l.file_uuid = h.uuid \n" +
            "${ew.customSqlSegment}")
    IPage<UserLogView> selectCurrentLogView(Page<UserLogView> page, @Param(Constants.WRAPPER) Wrapper<UserLogView> wrapper);

}